#include <REGX52.H>
#include "delay.h"

/** @bref 获得矩阵键盘按下的键值
  * @param 无
  * @retval 按下的键值
  */
unsigned char getMatrixKey()
{
	unsigned char keyNumber = 0;
	
	// 先把P1都置1
	P1 = 0xFF;
	// 再把P1_3置0
	P1_3 = 0;
	if(P1_7 == 0)
	{
		delay(20);while(P1_7 == 0);delay(20);keyNumber = 1;
	}
	if(P1_6 == 0)
	{
		delay(20);while(P1_6 == 0);delay(20);keyNumber = 5;
	}
	if(P1_5 == 0)
	{
		delay(20);while(P1_5 == 0);delay(20);keyNumber = 9;
	}
	if(P1_4 == 0)
	{
		delay(20);while(P1_4 == 0);delay(20);keyNumber = 13;
	}
	
	// 先把P1都置1
	P1 = 0xFF;
	// 再把P1_2置0
	P1_2 = 0;
	if(P1_7 == 0)
	{
		delay(20);while(P1_7 == 0);delay(20);keyNumber = 2;
	}
	if(P1_6 == 0)
	{
		delay(20);while(P1_6 == 0);delay(20);keyNumber = 6;
	}
	if(P1_5 == 0)
	{
		delay(20);while(P1_5 == 0);delay(20);keyNumber = 10;
	}
	if(P1_4 == 0)
	{
		delay(20);while(P1_4 == 0);delay(20);keyNumber = 14;
	}
	
	// 先把P1都置1
	P1 = 0xFF;
	// 再把P1_1置0
	P1_1 = 0;
	if(P1_7 == 0)
	{
		delay(20);while(P1_7 == 0);delay(20);keyNumber = 3;
	}
	if(P1_6 == 0)
	{
		delay(20);while(P1_6 == 0);delay(20);keyNumber = 7;
	}
	if(P1_5 == 0)
	{
		delay(20);while(P1_5 == 0);delay(20);keyNumber = 11;
	}
	if(P1_4 == 0)
	{
		delay(20);while(P1_4 == 0);delay(20);keyNumber = 15;
	}
	
	// 先把P1都置1
	P1 = 0xFF;
	// 再把P1_0置0
	P1_0 = 0;
	if(P1_7 == 0)
	{
		delay(20);while(P1_7 == 0);delay(20);keyNumber = 4;
	}
	if(P1_6 == 0)
	{
		delay(20);while(P1_6 == 0);delay(20);keyNumber = 8;
	}
	if(P1_5 == 0)
	{
		delay(20);while(P1_5 == 0);delay(20);keyNumber = 12;
	}
	if(P1_4 == 0)
	{
		delay(20);while(P1_4 == 0);delay(20);keyNumber = 16;
	}
	
	return keyNumber;
}